/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.capex.javaee.smtp;

import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

/**
 *
 * @author Acer
 */
public class SimpleMailReader {

    public static void main(String[] args) throws Exception {

        Properties mailProperties = new Properties();
        String hostMailServer = "pop.gmail.com";
        mailProperties.setProperty("mail.pop3.port", "995");
        mailProperties.put("mail.pop3.host", hostMailServer);
        mailProperties.put("mail.store.protocol", "pop3");
        mailProperties.put("mail.pop3.ssl.enable", "true");
        Session session = Session.getDefaultInstance(mailProperties);
        Store store = session.getStore();

        store.connect(hostMailServer, 995, "evegaluna@gmail.com", "");

        Folder inbox = store.getDefaultFolder().getFolder("INBOX");

        if (inbox == null) {
            System.out.println("No INBOX");
            System.exit(1);
        }

        inbox.open(Folder.READ_ONLY);
        Message[] messages = inbox.getMessages();
        for (int i= 0; i < messages.length; i++) {
            System.out.println("Message: "+(i+1));
            messages[i].writeTo(System.out);
        }

        inbox.close(false);
        store.close();

    }
}
